var ns, app;
app = new Vue({
    el: '#app',
    data: {
        ID: 0,
        form: {
            UseStatus: 0,//0-审核中 1-审核拒绝 2-未通行 3-已放行 4-已失效
            UseStatusName: '',
            OwnerName: '',
            PhoneNumber: '',
            ItemInformation: '',//物品信息
            VehicleInformation: '',//车辆信息
            VisitTypeName: '',//通行事由
            ReleaseInstructions: '',//放行说明
            StartTime: '',
            QrCodeImgPath: '',
        },
        imglist: [],
        //审批记录
        TrailList: [],
        IsLoadGuanJia: false,
        gjList: [],
        ReleaseStripImage: '',//电子放行条路径 
    },
    methods: {
        do_viewimages: function(activeIndex) {
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.imglist.length; i++) {
                imageUrls.push(that.imglist[i].imgurl);
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },
        get_data: function () {
            var that = this;
            var options = {};
            options.ID = that.ID;
            options.action = "getunderreview";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.form = data.data;
                    that.imglist = data.imglist;
                    that.TrailList = data.Model;
                    that.gjList = data.ps;
                    
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        //下载电子放行条
        download_erweima: function () {
            //ns.openFrame('outdoor_erweima');
            var that = this;
            ns.post({
                action: 'getmydownloadreleasenote',
                ID: that.ID,
            }, function (succeed, data, err) {
                if (succeed) {
                    that.ReleaseStripImage = data.url;
                    that.downReleaseStrip();
                } else if(err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
            })
        },
        downReleaseStrip: function () {
            var that = this;
            if (!that.ReleaseStripImage) {
                api.toast({
                    msg: '电子放行条路径为空',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            let timestamp=new Date().getTime();
            let format='fx'+timestamp+'.jpg';
            api.download({
                url:that.ReleaseStripImage,
                savePath:'fs://'+format,
                encode:false,
                report:false,
                cache:false,
                allowResume:true
            }, function(ret, err){
                if(ret.state==1){
                    api.saveMediaToAlbum({
                        path:'fs://'+format,
                    }, function(ret, err){
                        if(ret && ret.status){
                            api.toast({
                                msg: '保存电子放行条成功',
                                duration: 2000,
                                location: 'bottom'
                            });
                        }else{
                            api.toast({
                                msg: '保存失败',
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                        
                    });
                }
               
            })

        },
        //同意 拒绝
        do_open_surejujue: function (IsReject) {
            var that = this;
            ns.openWin('examine_single', '审批建议', {
                IsReject: IsReject,
                ID: that.ID,
            });
        },
        //手动失效
        doRevoke: function () {
            var that = this;
            api.confirm({
                title: '提示',
                msg: '确认手动失效?',
                buttons: ['确定失效', '取消']
            }, function(ret, err) {
                if (ret.buttonIndex==1) {
                    ns.post({
                        action: 'updatereleasestripstatus',
                        ID: that.ID,
                    }, function(succeed, data, err) {
                        if (succeed) {
                            api.toast({
                                msg: '操作成功',
                                duration: 2000,
                                location: 'bottom'
                            });
                            api.sendEvent({
                                name: 'do_load_data',
                            });
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    });
                }
            });
        },   
        //拨打电话 
        tel_phone: function(item) {
            api.call({
                type: 'tel_prompt',
                number: item.PhoneNumber
            });
        }, 
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;

    app.ID = api.pageParam.ID;
    app.get_data();

    api.addEventListener({
        name: 'do_load_data'
    }, function (ret, err) {
       app.get_data();
    });
};
